package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjy.pojo.Customer;
import com.ktjy.pojo.Linkman;
import com.ktjy.service.ICustomerService;
import com.ktjy.service.ILinkmanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-01-16
 */
@Controller
@RequestMapping("/linkman")
public class LinkmanController {
    Logger logger = LoggerFactory.getLogger(LinkmanController.class);

    @Autowired
    ILinkmanService iLinkmanService;
    @Autowired
    ICustomerService iCustomerService;

    @RequestMapping("/list")
    public String list(Linkman linkman,
                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       Model model) {
        try {
            //查询客户联系人信息
            QueryWrapper qw = new QueryWrapper();
            qw.eq("lkm_cust_no", linkman.getLkmCustNo());
            List<Linkman> linkmanList=iLinkmanService.list(qw);
            model.addAttribute("linkmanList",linkmanList);

            //查询客户信息
            Customer customer=iCustomerService.getById(linkman.getLkmCustNo());
            model.addAttribute("customer",customer);
        } catch (Exception e) {
            logger.error("LinkmanController 中的 list 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "linkman/list";
    }

    /**
     * 跳转到新增页面
     *
     * @return
     */
    @GetMapping("/add")
    public String add(@RequestParam(value = "custNo") String custNo,
                      @RequestParam(value = "custName") String custName,
                      Model model) {
        try {
            model.addAttribute("custNo",custNo);
            model.addAttribute("custName",custName);
        } catch (Exception e) {
            logger.error("LinkmanController 中的 add 方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "linkman/add";
    }

    @PostMapping("/save")
    public String save(Linkman linkman) {
        try {
            boolean ok = iLinkmanService.save(linkman);
            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/linkman/list?lkmCustNo="+linkman.getLkmCustNo();
            } else {
                //跳回新增页面
                return "redirect:/linkman/add";
            }
        } catch (Exception e) {
            logger.error("LinkmanController 中的 save 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 跳转修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/edit")
    public String edit(@RequestParam("id") Long id, Model model) {
        try {
            //根据ID，查询联系人信息
            Linkman linkman=iLinkmanService.getById(id);
            model.addAttribute("linkman",linkman);
        } catch (Exception e) {
            logger.error("LinkmanController 中的 toEdit 方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "linkman/edit";
    }

    @PostMapping("/update")
    public String update(Linkman linkman) {
        try {
            boolean ok = iLinkmanService.updateById(linkman);
            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/linkman/list?lkmCustNo="+linkman.getLkmCustNo();
            } else {
                //跳回新增页面
                return "redirect:/linkman/edit";
            }
        } catch (Exception e) {
            logger.error("LinkmanController 中的 update 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            boolean ok = iLinkmanService.removeById(id);  //调用service的方法进行数据删除
            if (ok) {
                map.put("delResult", true);
            } else {
                map.put("delResult", false);
            }
        } catch (Exception e) {
            logger.error("LinkmanController 中的 del 方法执行发生错误。", e);
            map.put("delResult", false);
        }
        return map;
    }
}
